from itsdangerous import TimedJSONWebSignatureSerializer as  Serializer
from django.conf import settings


def generic_email_token(email, user_id):
    """邮箱加密"""
    # secret_key,  密钥
    # expires_in=None 过期时间
    # 1. 创建实例对象
    s = Serializer(secret_key=settings.SECRET_KEY, expires_in=3600)
    # 2. 组织数据
    data = {
        'email': email,
        'user_id': user_id
    }
    # 3. 数据加密
    secret_data = s.dumps(data)
    # 4. 返回数据
    return secret_data.decode()

from itsdangerous import BadTimeSignature
def chect_email_token(token):
    # 1.创建实例对象
    s = Serializer(secret_key=settings.SECRET_KEY,expires_in=3600)
    # 2.解密
    try:
        data = s.loads(token)
    except BadTimeSignature:
        return None
    else:
        # 3. 返回解密数据
        return data
